2 QUnit
.module( 'mediawiki.user', QUnit
.newMwEnvironment( {
4 this.server
= this.sandbox
.useFakeServer();
5 // Cannot stub by simple assignment because read-only.
6 // Instead, stub in tests by using 'delete', and re-create
7 // in teardown using the original descriptor (including its
8 // accessors and readonly settings etc.)
9 this.crypto
= Object
.getOwnPropertyDescriptor( window
, 'crypto' );
10 this.msCrypto
= Object
.getOwnPropertyDescriptor( window
, 'msCrypto' );
12 teardown: function () {
14 Object
.defineProperty( window
, 'crypto', this.crypto
);
16 if ( this.msCrypto
) {
17 Object
.defineProperty( window
, 'msCrypto', this.msCrypto
);
22 QUnit
.test( 'options', function ( assert
) {
23 assert
.ok( mw
.user
.options
instanceof mw
.Map
, 'options instance of mw.Map' );
26 QUnit
.test( 'getters (anonymous)', function ( assert
) {
27 // Forge an anonymous user
28 mw
.config
.set( 'wgUserName', null );
29 mw
.config
.set( 'wgUserId', null );
31 assert
.strictEqual( mw
.user
.getName(), null, 'getName()' );
32 assert
.strictEqual( mw
.user
.isAnon(), true, 'isAnon()' );
33 assert
.strictEqual( mw
.user
.getId(), 0, 'getId()' );
36 QUnit
.test( 'getters (logged-in)', function ( assert
) {
37 mw
.config
.set( 'wgUserName', 'John' );
38 mw
.config
.set( 'wgUserId', 123 );
40 assert
.strictEqual( mw
.user
.getName(), 'John', 'getName()' );
41 assert
.strictEqual( mw
.user
.isAnon(), false, 'isAnon()' );
42 assert
.strictEqual( mw
.user
.getId(), 123, 'getId()' );
44 assert
.strictEqual( mw
.user
.id(), 'John', 'user.id()' );
47 QUnit
.test( 'getUserInfo', function ( assert
) {
48 mw
.config
.set( 'wgUserGroups', [ '*', 'user' ] );
50 mw
.user
.getGroups( function ( groups
) {
51 assert
.deepEqual( groups
, [ '*', 'user' ], 'Result' );
54 mw
.user
.getRights( function ( rights
) {
55 assert
.deepEqual( rights
, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' );
58 mw
.user
.getRights().done( function ( rights
) {
59 assert
.deepEqual( rights
, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' );
62 this.server
.respondWith( /meta=userinfo/, function ( request
) {
63 request
.respond( 200, { 'Content-Type': 'application/json' },
64 '{ "query": { "userinfo": { "groups": [ "unused" ], "rights": [ "read", "edit", "createtalk" ] } } }'
68 this.server
.respond();
71 QUnit
.test( 'generateRandomSessionId', function ( assert
) {
74 result
= mw
.user
.generateRandomSessionId();
75 assert
.strictEqual( typeof result
, 'string', 'type' );
76 assert
.strictEqual( result
.trim(), result
, 'no whitespace at beginning or end' );
77 assert
.strictEqual( result
.length
, 20, 'size' );
79 result2
= mw
.user
.generateRandomSessionId();
80 assert
.notEqual( result
, result2
, 'different when called multiple times' );
84 QUnit
.test( 'generateRandomSessionId (fallback)', function ( assert
) {
87 // Pretend crypto API is not there to test the Math.random fallback
89 delete window
.msCrypto
;
90 // Assert that the above actually worked. If we use the wrong method
91 // of stubbing, JavaScript silently continues and we need to know that
92 // it was the wrong method. As of writing, assigning undefined is
93 // ineffective as the window property for Crypto is read-only.
94 // However, deleting does work. (T203275)
95 assert
.strictEqual( window
.crypto
|| window
.msCrypto
, undefined, 'fallback is active' );
97 result
= mw
.user
.generateRandomSessionId();
98 assert
.strictEqual( typeof result
, 'string', 'type' );
99 assert
.strictEqual( result
.trim(), result
, 'no whitespace at beginning or end' );
100 assert
.strictEqual( result
.length
, 20, 'size' );
102 result2
= mw
.user
.generateRandomSessionId();
103 assert
.notEqual( result
, result2
, 'different when called multiple times' );
106 QUnit
.test( 'getPageviewToken', function ( assert
) {
107 var result
= mw
.user
.getPageviewToken(),
108 result2
= mw
.user
.getPageviewToken();
109 assert
.strictEqual( typeof result
, 'string', 'type' );
110 assert
.strictEqual( /^[a-f0-9]{20}$/.test( result
), true, '20 HEX symbols string' );
111 assert
.strictEqual( result2
, result
, 'sticky' );
114 QUnit
.test( 'sessionId', function ( assert
) {
115 var result
= mw
.user
.sessionId(),
116 result2
= mw
.user
.sessionId();
117 assert
.strictEqual( typeof result
, 'string', 'type' );
118 assert
.strictEqual( result
.trim(), result
, 'no leading or trailing whitespace' );
119 assert
.strictEqual( result2
, result
, 'retained' );